package com.atguigu.controller;

import com.atguigu.base.BaseController;
import com.atguigu.entity.*;
import com.atguigu.service.*;
import com.github.pagehelper.PageInfo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/house")
public class HouseController extends BaseController {
    @DubboReference
    private HouseService houseService;

    @DubboReference
    private DictService dictService;

    @DubboReference
    private CommunityService communityService;

    @DubboReference
    private HouseImageService houseImageService;

    @DubboReference
    private HouseBrokerService houseBrokerService;

    @DubboReference
    private HouseUserService houseUserService;
    /*
    * 带条件分页查询房产信息
    * 渲染 house/index
    * */
    @RequestMapping
    public String index(HttpServletRequest request,Map map){
        Map<String, Object> filters = getFilters(request);

        // 根据查询分页信息
        PageInfo<House> page = houseService.findPage(filters);
        map.put("page",page);
        // 查询条件放入请求域
        map.put("filters",filters);


        map.put("communityList",communityService.findAll());
        map.put("houseTypeList",dictService.findListByDictCode("houseType"));
        map.put("floorList",dictService.findListByDictCode("floor"));
        map.put("buildStructureList",dictService.findListByDictCode("buildStructure"));
        map.put("directionList",dictService.findListByDictCode("direction"));
        map.put("decorationList",dictService.findListByDictCode("decoration"));
        map.put("houseUseList",dictService.findListByDictCode("houseUse"));
        return "house/index";
    }

    /*渲染新增房源信息的视图*/
    @RequestMapping("/create")
    public String create(Map map){
        map.put("communityList",communityService.findAll());
        map.put("houseTypeList",dictService.findListByDictCode("houseType"));
        map.put("floorList",dictService.findListByDictCode("floor"));
        map.put("buildStructureList",dictService.findListByDictCode("buildStructure"));
        map.put("directionList",dictService.findListByDictCode("direction"));
        map.put("decorationList",dictService.findListByDictCode("decoration"));
        map.put("houseUseList",dictService.findListByDictCode("houseUse"));
        return "house/create";
    }

    /*接收新的房源信息保存进入数据库
    * 渲染成功页*/
    @RequestMapping("/save")
    public String save(House house){
        // 调用服务层方法保存视图
        houseService.insert(house);
        return  "common/successPage";
    }

    /*渲染修改页面的信息*/
    @RequestMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, Model model){
        // 调用服务层方法,根据id获取房源信息
        House house = houseService.getById(id);
        model.addAttribute("house",house);
        // 将房源信息放入请求域

        model.addAttribute("communityList",communityService.findAll());
        model.addAttribute("houseTypeList",dictService.findListByDictCode("houseType"));
        model.addAttribute("floorList",dictService.findListByDictCode("floor"));
        model.addAttribute("buildStructureList",dictService.findListByDictCode("buildStructure"));
        model.addAttribute("directionList",dictService.findListByDictCode("direction"));
        model.addAttribute("decorationList",dictService.findListByDictCode("decoration"));
        model.addAttribute("houseUseList",dictService.findListByDictCode("houseUse"));
        // 返回视图
        return "house/edit";
    }


    /*接收要更新的房源信息
    * 渲染成功页
    * */
    @RequestMapping("/update")
    public String update(House house){
        // 调用服务层方法更新房源信息
        houseService.update(house);
        return "common/successPage";
    }

    /*删除房源
    * 渲染房源的index 首页
    **/

    @RequestMapping("/delete/{id}")
    public String delete(@PathVariable("id")Long id){
        houseService.delete(id);
        return "redirect:/house";
    }

    /*发布和取消发布的处理器
    * 重定向到房源信息展示页
    * */
    @RequestMapping("/publish/{id}/{status}")
    public String publish(
            @PathVariable("id") Long id,
            @PathVariable("status") int status
    ){
        houseService.publish(id,status);
        return "redirect:/house";
    }

    /*展示房源详情页*/
    @RequestMapping("/{id}")
    public String show(@PathVariable("id") Long id,Map map){
        //查询各种信息放入请求域
        House house = houseService.getById(id);
        Community community = communityService.getById(house.getCommunityId());
        // 查询房产图片和房源图片
        List<HouseImage> houseImage1List = houseImageService.findList(house.getId(), 1);
        List<HouseImage> houseImage2List = houseImageService.findList(house.getId(), 2);

        // 查询房产经纪人信息
        List<HouseBroker> houseBrokerList = houseBrokerService.findListByHouseId(house.getId());
        // 查询业主信息
        List<HouseUser> houseUserList = houseUserService.findListByHouseId(house.getId());


        map.put("house",house);
        map.put("community",community);
        map.put("houseImage1List",houseImage1List);
        map.put("houseImage2List",houseImage2List);
        map.put("houseBrokerList",houseBrokerList);
        map.put("houseUserList",houseUserList);
        //返回show视图
        return "house/show";

    }


}
